library(readr)
library(ggplot2)
library(scales)
library(tidyverse)
library(cowplot)
library(bdscale)
library(tidytext)
library(jsonlite)
library(cowplot)

### abortion total
options(scipen=999)
results1 <- read_csv(here::here("data","results_all_may.csv"))
results2 <- read_csv(here::here("data","results_all_june.csv"))

results <- rbind(results1, results2)

results$day <- as.Date(results$day)

results <- results %>%
  group_by(day) %>%
  dplyr::summarise(count = sum(total))

results$tempvar <- "A. Engagement with Tweets About Abortion"

results$Period <- c("Leak", "Leak", "Leak", "Ruling","Ruling","Ruling")
a<-ggplot(results, aes(x=day, y=count, color = Period)) +
  geom_line(aes(color=Period), size=1) + 
  ylab("Engagement") +
  theme(axis.text.x=element_text(angle=60, hjust=1)) +
  scale_y_continuous(labels = comma, breaks = seq(0,90000000000,10000000000), limits = c(0,90000000000)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"),axis.text.y = element_text(face="bold", colour = "black")) +
  scale_x_bd(business.dates=results$day, labels=date_format('%B %d'),
             max.major.breaks=6 )+
  annotate("text", x = as.Date("2022-05-02"), y = 10000000000 , label = paste("Max engagements\n", format(max(results$count[results$Period=="Leak"]), format="f", big.mark=",", digits=1))) +
  annotate("text", x = as.Date("2022-06-24"), y = 85000000000 , label = paste("Max engagements\n", format(max(results$count[results$Period!="Leak"]), format="f", big.mark=",", digits=1))) +
  facet_grid(. ~ tempvar) +
  theme(strip.background = element_rect(fill="#000000"),
        strip.text = element_text(size=15, colour="white"))+
  theme(legend.position = "bottom") +
  xlab("") 
a

### Polls engagement

results <- read_csv(here::here("data","results_polls.csv"))

results_day <- results %>%
  group_by(url) %>%
  summarise(count = sum(total))

results$day <- as.Date(results$day)

results_day <- results %>%
  group_by(day) %>%
  dplyr::summarise(count = sum(total))

pre <- mean(results_day$count[results_day$day < as.Date("2022-05-02")])
results_day$tempvar <- "C. Number of Tweets About Abortion Polling"
c<-ggplot(results_day, aes(x=day, y=count)) +
   # geom_vline(xintercept = as.Date("2022-05-02"), linetype="solid", color = "#D55E00") +
   # geom_vline(xintercept = as.Date("2022-06-24"), linetype="solid", color = "#D55E00") +
  geom_line(color="#0072B2", size=1) + 
  xlab("") +
  ylab("Tweets") +
  theme(axis.text.x=element_text(angle=60, hjust=1)) +
  scale_y_continuous(labels = comma, breaks = seq(0,4500000,250000)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"),axis.text.y = element_text(face="bold", colour = "black")) +
  annotate("text", x = as.Date("2022-4-18"), y = 250000, label = "Leak ->") +
  annotate("text", x = as.Date("2022-6-04"), y = 250000, label = "Ruling ->") +
  scale_x_date(date_labels = "%B") +facet_grid(. ~ tempvar) +
  theme(strip.background = element_rect(fill="#000000"),
        strip.text = element_text(size=15, colour="white"))
#ggsave("plots/trend.pdf", width=9, height=4.5)

# sentiment polls

results2 <- read_csv(here::here("data","sentiment_polls.csv"))

results2$s2 <- c("Negative", "Positive")
results2$tempvar <- "D. Sentiment of Polling Tweets"
d<-results2 %>%  ggplot(aes(y=freq, x=s2, fill=s2)) + 
  geom_bar(stat="identity", fill = c("#D55E00",  "#009E73")) +
  theme_bw() + 
  theme(axis.text.y = element_text(size = 12),
                     axis.text.x = element_text(size = 12),
                     axis.title.x = element_text(size = 12, vjust = -.1),
                     panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank()) +
  xlab("Sentiment") +
  ylab("Proportion of Poll Tweets")  +
  facet_grid(. ~ tempvar) +
  theme(strip.background = element_rect(fill="#000000"),
        strip.text = element_text(size=15, colour="white"))
 
# sentiment all

results2 <- read_csv(here::here("data","abortion_tweets.csv"))

results2$s2 <- c("Negative", "Positive")
results2$tempvar <- "B. Sentiment of Abortion Tweets"
b<-results2 %>%  ggplot(aes(y=freq, x=s2, fill=s2)) + 
  geom_bar(stat="identity", fill = c("#D55E00",  "#009E73")) +
  theme_bw() + 
  theme(axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        axis.title.x = element_text(size = 12, vjust = -.1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlab("Sentiment") +
  ylab("Proportion of Poll Tweets")  +
  facet_grid(. ~ tempvar) +
  theme(strip.background = element_rect(fill="#000000"),
        strip.text = element_text(size=15, colour="white"))


title <- ggdraw() + draw_label("Perceptions of American Moral Opposition to Abortion", fontface='bold')             
#d <-ggplot(mtcars, aes(x = wt, y = mpg)) + theme_void()
plotr1 <- plot_grid(a, c, label_size = 12, ncol=1,rel_heights=c(1,1))
plotr2 <- plot_grid(b, d, label_size = 12, ncol=1,rel_heights=c(1,1))
(plot1 <- plot_grid(plotr1, plotr2,  ncol = 2,rel_widths=c(1, .75)))

ggsave(plot = plot1, "plots/plot5.pdf", width=9.5, height=8.5)
  